//
//  InitializerA.swift
//  swiftStudy
//
//  Created by unisedu on 16/4/1.
//  Copyright © 2016年 CaoFei. All rights reserved.
//

import UIKit
//构造器(Initializer)
//为了初始化结构体和类等类型的实力属性
// 默认构造器 ,没有参数没有返回值

struct FahrenHeit {
    var temperature : Double
    //也可以不写init，但创建对象时需要人工扶植
    init (){
        temperature = 32.0
    }
}
// 自定义构造器
struct Celsius {
    var temperatureInCelsius : Double = 0.0
    init(fromFahrenheit fahrenheit : Double ,aaccc aaa :Int ) {
        temperatureInCelsius = (fahrenheit - 32.0) / 1.8
    }
    init(fromKelvin kelvin : Double) {
        temperatureInCelsius = kelvin - 237.15
    }
    
}
//析构器(Deinitializer) 与构造器相反 ，在对象释放时候调用
//使用关键字deinit
//deinit{
//    //perform the deinitialization
//}

class Player {
    var coinsInPurse : Int
    init(coins : Int) {
        print ("call init")
        coinsInPurse = coins
    }
    func winCoins(coins : Int){
        coinsInPurse += 10
    }
    deinit {
        coinsInPurse = 0
    }
}

class InitializerA: NSObject {
    func show(){
        var f = FahrenHeit()
        print  ("The default temperature is \(f.temperature) Fahrenheit")
        let boilingPointOfWater = Celsius (fromFahrenheit : 212.0 ,aaccc : 1)
        let freezingPointOfWater = Celsius (fromKelvin : 273.15)
        //必须是可选类型（Player?）才能nil
        var playerOne : Player? = Player (coins : 100)
        print ("coinsInPurse : \(playerOne!.coinsInPurse) coins")
        //必须是可选类型才能nil
        playerOne = nil //这种情况下调用deinit
        print ("PlayerOne has left the game")

    }
}






